package hl.service.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import hl.dao.BaseDao;
import hl.model.TCard;
import hl.model.TCoach;
import hl.model.TGroupCourse;
import hl.model.TSalary;
import hl.model.TStuff;
import hl.model.TTraining;
import hl.pageModel.DataGrid;
import hl.pageModel.Salary;
import hl.service.SalaryService;

@Service("salaryService")
public class SalaryServiceImpl implements SalaryService {

	private BaseDao<TSalary> salaryDao;

	public BaseDao<TSalary> getSalaryDao() {
		return salaryDao;
	}

	@Autowired
	public void setSalaryDao(BaseDao<TSalary> salaryDao) {
		this.salaryDao = salaryDao;
	}

	private BaseDao<TStuff> stuffDao;

	public BaseDao<TStuff> getStuffDao() {
		return stuffDao;
	}

	@Autowired
	public void setStuffDao(BaseDao<TStuff> stuffDao) {
		this.stuffDao = stuffDao;
	}

	private BaseDao<TCoach> coachDao;

	public BaseDao<TCoach> getCoachDao() {
		return coachDao;
	}

	@Autowired
	public void setCoachDao(BaseDao<TCoach> coachDao) {
		this.coachDao = coachDao;
	}

	@Override
	public DataGrid getDatagrid(Salary salary) {
		DataGrid dg = new DataGrid();
		String hql = "from TSalary t ";

		Map<String, Object> params = new HashMap<String, Object>();

		// hql = search(salary, hql, params);

		if (salary.getOrder() != null) {
			hql += " order by " + salary.getSort() + " " + salary.getOrder();
		}

		List<TSalary> l = salaryDao.find(hql, params, salary.getPage(), salary.getRows());

		int totalHql = salaryDao.count(hql, params);

		List<Salary> pl = new ArrayList();
		if (l != null && l.size() > 0) {
			for (TSalary t : l) {
				Salary u = new Salary();
				BeanUtils.copyProperties(t, u);
				if (t.getTStuff() != null) {
					u.setPkStuff(t.getTStuff().getPkStuff());
					u.setStuffName(t.getTStuff().getName());
				}
				if (t.getTCoach() != null) {
					u.setPkCoach(t.getTCoach().getPkCoach());
					u.setCoachName(t.getTCoach().getName());
				}
				pl.add(u);
			}
		}
		dg.setRows(pl);
		dg.setTotal(totalHql);
		return dg;
	}

	private String search(Salary salary, String hql, Map<String, Object> params) {
		/*
		 * if (salary.getName() != null && !salary.getName().trim().equals("")) { hql += " where t.name like :value1 "; params.put("value1", "%" + salary.getName().trim() + "%"); }
		 * 
		 * if (salary.getGender() != null && !salary.getGender().equals("")) { if (hql.contains("where")) hql += " and "; else hql += " where "; hql += " t.gender like :value2 "; params.put("value2", "%" + salary.getGender().trim() + "%"); } if (salary.getPksalary() != null && !salary.getPksalary().equals("")) { if (hql.contains("where")) hql += " and "; else hql += " where "; hql += " t.pksalary like :value3 "; params.put("value3", "%" + salary.getPksalary().toString() + "%"); }
		 */
		return hql;
	}

	@Override
	public void save(Salary salary) {
		String date = salary.getYear() + "-" + salary.getMonth();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date calDate = new Date();
		Calendar x = Calendar.getInstance();
		x.setTime(calDate);
		x.add(Calendar.DAY_OF_MONTH, -30);
		calDate = x.getTime();
		List<TStuff> stuff = stuffDao.find("from TStuff where workStatus=?", new Object[] { "在职" });
		List<TCoach> coach = coachDao.find("from TCoach where workStatus=?", new Object[] { "在职" });
		for (TStuff s : stuff) {
			TSalary t = new TSalary();
			t.setDate(date);
			int cash = 2800;
			Set<TCard> sa = s.getTCards();
			for (TCard a : sa) {
				if (a.getCdatetime().compareTo(calDate) > 0) {
					if (a.getCardType().compareTo("月卡") == 0)
						cash += 20;
					else if (a.getCardType().compareTo("季卡") == 0)
						cash += 80;
					else if (a.getCardType().compareTo("半年卡") == 0)
						cash += 200;
					else if (a.getCardType().compareTo("年卡") == 0)
						cash += 500;
					else if (a.getCardType().compareTo("两年卡") == 0)
						cash += 1200;
				}
			}
			t.setCash(cash);
			t.setTStuff(s);
			t.setCdatetime(new Date());
			salaryDao.save(t);
		}
		for (TCoach c : coach) {
			TSalary t = new TSalary();
			t.setDate(date);
			int cash = 3500;
			Set<TGroupCourse> cg = c.getTGroupCourses();
			Set<TTraining> ct = c.getTTrainings();
			int gcc=0;
			int tcc=0;
			for (TGroupCourse cgg:cg) {
				if(cgg.getBeginDatetime().compareTo(calDate)>0)
					gcc++;
			}
			for (TTraining tgg:ct) {
				if(tgg.getBeginDatetime().compareTo(calDate)>0)
					tcc++;
			}
			cash+=gcc*250;
			cash+=tcc*180;
			t.setCash(cash);
			t.setTCoach(c);
			t.setCdatetime(new Date());
			salaryDao.save(t);
		}

	}

}
